package cn.ncu.controller;

import cn.ncu.entity.Place;
import cn.ncu.entity.Result;
import cn.ncu.service.PlaceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Base64Utils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @author:Martina
 * @create:2020-07-16 15:11
 * @description:
 */
@RestController
@CrossOrigin
@RequestMapping("place")
public class PlaceController {

    @Autowired
    private PlaceService placeService;


    @GetMapping("findAll")
    public Result findAll(Integer page,Integer rows,String provinceId){
        Result result = new Result();
        page = page == null ? 1 : page;
        rows = rows == null ? 6 : rows;
        List<Place> places = placeService.findByProvinceIdPage(page, rows, provinceId);
        Integer counts = placeService.findByProvinceIdCounts(provinceId);
        Integer totalsPage = counts % rows == 0 ? counts / rows : counts / rows + 1;
        result.setPlaces(places).setFlag(true).setPage(page).setTotalsPage(totalsPage).setTotals(counts);
        return result;
    }

    @PostMapping("save")
    public Result saveProvince(MultipartFile file,Place place){
        Result result = new Result();
//        String realPath = "D:\\PersonalFiles\\DownLoad\\images";
//        String extension = FilenameUtils.getExtension(file.getOriginalFilename());
//        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        LocalDate now = LocalDate.now();
//        String format = dateTimeFormatter.format(now);
//        String newname = (format + UUID.randomUUID().toString()).replace("-", "") + "." + extension;
//        File file1 = new File(realPath, newname);
//        try {
//            file.transferTo(new File(realPath,newname));
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
        try {
            place.setPicpath(Base64Utils.encodeToString(file.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            placeService.save(place);
            result.setFlag(true).setMessage("保存成功！");
        } catch (Exception e) {
            e.printStackTrace();
            result.setMessage("保存失败，请稍后重试！");
        }
        return result;
    }

    @GetMapping("findProvinces")
    public Result findProvinces(){
        Result result = new Result();
        List<String> provinces = placeService.findProvinces();
        result.setProvincesName(provinces).setFlag(true);
        return result;
    }

    @GetMapping("delete")
    public Result deleteById(String id){
        Result result = new Result();
        try {
            placeService.delete(id);
            result.setFlag(true);
        } catch (Exception e) {
            result.setMessage("删除失败，请稍后重试！");
        }
        return result;
    }

}
